package com.example.demospringboot.plugins;

import com.example.demospringboot.annotations.ApiOperationSince;
import com.google.common.base.Optional;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.service.ObjectVendorExtension;
import springfox.documentation.service.StringVendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

import java.util.Collections;

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1002)
@Slf4j
public class SinceOperationBuilder implements OperationBuilderPlugin {

    private static Logger logger = LoggerFactory.getLogger(SinceOperationBuilder.class);
    @Override
    public void apply(OperationContext context) {
        Optional<ApiOperationSince> annotation =
                context.findAnnotation(ApiOperationSince.class);
        if (annotation.isPresent() && annotation.get().value() != null) {
            logger.info("start1111111111111111111111");
            String value = annotation.get().value();
            String description = annotation.get().description();
            ObjectVendorExtension ext = new ObjectVendorExtension("Since");
            if (description != null) {
                ext.addProperty(
                        new StringVendorExtension("description", description));
            }
            ext.addProperty(new StringVendorExtension("value", value));

            context.operationBuilder().extensions(
                    Collections.singletonList(ext));
            log.debug("Added since ", value);
            logger.info("start122222222222" + value  + " des is " + description);
        }
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
